
public class e034 {
    public static void main(String[] args) {
        long sTime = System.currentTimeMillis();
        int sum = 0;
        for (int i = 3; i < 50000; i++) {
            if (hasProp(i)) {
                sum += i;
            }
        }
        System.out.println(sum);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }

    static boolean hasProp(int i) {
        String l = String.valueOf(i);
        int sum = 0;
        for (int j = 0; j < l.length(); j++) {
            int n = Integer.parseInt(String.valueOf(l.charAt(j)));
            sum += factorial(n);
        }
        return (sum == i);
    }

    static int factorial(int n) {
        if (n == 0) {
            return 1;
        }
        int result = n;
        for (int i = n - 1; i > 0; i--) {
            result *= i;
        }
        return result;
    }
}